request = require 'request'
async = require 'async'
fs = require 'fs'
a2z = [122..97]
num = [0..990]

shengmu = "b p m f d t n l g k h j q x zh ch sh r z c s y w".split(' ')
yunmu = "a o e i u ai ei ui ao ou ia ie ue er an en in un ang eng ing ong".split(' ')
pinyin = []
for s in shengmu
	for y in yunmu
		pinyin.push s+y



# arry = []
# for i in a2z
# 	for j in a2z
# 		for k in a2z
# 			arry.push "#{String.fromCharCode(i)}#{String.fromCharCode(j)}#{String.fromCharCode(k)}"
# console.log arry
# console.log (String.fromCharCode(alpha) for alpha in a2z)
all = []
for a in pinyin
	for b in pinyin
		all.push a+b

index = fs.readFileSync 'domain-num.txt'

# console.log all.length
async.forEachLimit all, 2, (n, cb)->
	# n = String.fromCharCode 

	if all.indexOf(n) < parseInt index.toString()
		return setImmediate ()->
			cb null
	if n.length > 6
		return setImmediate ()->
			cb null
	url = "http://pandavip.www.net.cn/check/check_ac1.cgi?domain=#{n}.com&_=#{Math.random()}"
	request.get url, (err, res, body)->
		console.log err if err
		console.log body
		console.log all.indexOf n

		if body.length > 1000
			return setTimeout ()->
				cb null
			,2000
		if body.indexOf('name is available')>0
			console.log body
			fs.appendFileSync 'domain.txt', body+'\n'
		unless err
			fs.writeFileSync "domain-num.txt", all.indexOf n
		cb null	
	# url = "http://www.idcicp.com/domain/domain_query.asp"
	# console.log n
	# request.post 
	# 	url: url
	# 	form: 
	# 		domainname:"#{n}.im"

	# ,(err, res, body)->
	# 	if body is 'Y'
	# 		console.log n+ '-------------get'
	# 		fs.appendFileSync 'domain.txt', "#{n}.im"+'\n'
	# 	cb null

	